package org.dromara.wms.service;

import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.web.core.BaseService;
import org.springframework.stereotype.Service;
import org.dromara.wms.domain.WmsProduct;
import org.dromara.wms.mapper.WmsProductMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产品管理Service业务层处理
 *
 * @author wenrui
 * @date 2024-03-20
 */
@RequiredArgsConstructor
@Service
public class WmsProductServiceImpl extends BaseService {

    private final WmsProductMapper baseMapper;
    private final WmsProductOssServiceImpl ossService;

    /**
     * 查询产品管理
     */
    public WmsProduct queryById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询产品管理列表
     */
    public TableDataInfo<WmsProduct> queryPageList(WmsProduct bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WmsProduct> lqw = buildQueryWrapper(bo);
        Page<WmsProduct> result = baseMapper.selectPage(pageQuery.build(), lqw);
        if (bo.getIds() != null && !bo.getIds().isEmpty()) {
            List<WmsProduct> records = result.getRecords();
            List<Long> parentIds = bo.getIds();
            records.removeIf(wmsProduct -> parentIds.contains(wmsProduct.getId()));
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品管理列表
     */
    public List<WmsProduct> queryList(WmsProduct bo) {
        LambdaQueryWrapper<WmsProduct> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<WmsProduct> buildQueryWrapper(WmsProduct bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WmsProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSku()), WmsProduct::getSku, bo.getSku());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WmsProduct::getName, bo.getName());
        lqw.eq(bo.getIsStock() != null, WmsProduct::getIsStock, bo.getIsStock());
        lqw.eq(StringUtils.isNotBlank(bo.getTreeValue()), WmsProduct::getTreeValue, bo.getTreeValue());
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
                WmsProduct::getUpdateTime, params.get("beginUpdateTime"), params.get("endUpdateTime"));
        return lqw;
    }

    /**
     * 新增产品管理
     */
    public Boolean insertByBo(WmsProduct bo) {
        validEntityBeforeSave(bo);
        updateProductOss(bo);
        return baseMapper.insert(bo) > 0;
    }

    /**
     * 修改产品管理
     */
    public Boolean updateByBo(WmsProduct bo) {
        validEntityBeforeSave(bo);
        updateProductOss(bo);
        return baseMapper.updateById(bo) > 0;
    }

    private void updateProductOss(WmsProduct productBo){
      if (!productBo.getProductOss().isEmpty())
        ossService.insertByArray(productBo.getProductOss());
      else
        ossService.deleteByProductId(productBo.getId());
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WmsProduct entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产品管理
     */
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
